﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MedicalManagementSystem.Utils;

namespace MedicalManagementSystem.Models
{
    public partial class Disability : IValidatable
    {
        public static Disability GetByPK(int id)
        {
            return DbContext.Instance.Disability.Where(disability => disability.id == id).FirstOrDefault();
        }

        public void LoadPatient()
        {
            if (EntityState != System.Data.EntityState.Added && EntityState != System.Data.EntityState.Detached)
            {
                if (!PatientReference.IsLoaded)
                {
                    PatientReference.Load();
                }
            }
        }

        public static readonly IEnumerable<int> AvailableGroupNumb = new[] {1, 2, 3, 4}; 

        public string GroupView()
        {
            return GroupView(this);
        }

        public static string GroupView(int groupNumb)
        {
            switch (groupNumb)
            {
                case 1:
                    return "I";
                case 2:
                    return "II";
                case 3:
                    return "III";
                case 4:
                    return "IV";
            }
            return "UNKNOWN";
        }

        public static string GroupView(Disability disability)
        {
            return GroupView(disability.GroupNumb);
        }

        public bool CheckBeforeSave()
        {
            return GetRuleViolations().Count() == 0;
        }

        public IEnumerable<IRuleViolation> GetRuleViolations()
        {
            if (DateStart == DateTime.MinValue)
            {
                yield return new RuleViolation("Неверный формат даты", this, "DateStart", DateStart);
            }
            if (GroupNumb == 0)
            {
                yield return new RuleViolation("Выбирите группу инвалидности", this, "GroupNumb", GroupNumb);    
            }
        }
    }
}
